<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH . 'models/model_kontak.php');
require_once(APPPATH . 'models/model_grup.php');

class Kontak extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}

	# ========== LIHAT ==========

	public function lihat($mulai = 0)
	{
		$this->load->library('table');
		$this->load->library('pagination');

		$config = $this->config->item('pagination');

		$config['base_url']=  site_url('/kontak/lihat/');
		$config['total_rows']=  model_kontak::count();
		$config['per_page']=  50;

		$this->pagination->initialize($config);

		$data['halaman'] = $this->pagination->create_links();
		$data['mulai'] = $mulai;
		$data['kontak_array'] = model_kontak::read(array(), array($config['per_page'], $mulai), '*', false, "", "nama ASC");

		$data['grup_array'] = model_grup::read();

		$this->load->view('theme/theme_kepala');
		$this->load->view('kontak/kontak_lihat', $data);
		$this->load->view('theme/theme_kaki');
	}

	# ========== TAMBAH ==========

	public function tambah()
	{
		$data = array(
					'url'=>'/kontak/tambah_proses',
					'isi_form'=>array(
									'pbk_id'	=>"",
									'nama'		=>"",
									'nomor'		=>"",
									'keterangan'=>"",
									'grup_id'	=>"",
								),
					'grup_array'=>model_grup::read(),
				);

		$this->load->view('theme/theme_kepala');
		$this->load->view('kontak/kontak_formulir', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function tambah_proses(){
		$data = $this->input->post();
		$grup_id_array = $data['grup_id'];
		
		unset($data['grup_id']);

		// menambahkan kontak
		$pbk_id = model_kontak::create($data);

		// menambahkan relasi kontak-grup
		model_crud::$tabel="pbk_relasi";
		model_crud::$tabel_join=array();

		$relasi = array('pbk_id' => $pbk_id);

		foreach ($grup_id_array as $grup_id) {
			$relasi['grup_id'] = $grup_id;
			model_crud::create($relasi);
		}

		redirect(site_url('/kontak/lihat'), 'refresh');
	}

	# ========== UBAH ==========

	public function ubah($id)
	{
		if ($id=="") show_404();

		$form_isi = model_kontak::read(array('pbk_id'=>$id))[$id];

		// jika kontak tidak ditemukan
		if ($form_isi===false) show_404();

		$data = array(
					'url'=>'/kontak/ubah_proses',
					'isi_form'=>array(
									'pbk_id'	=>$form_isi->get_id(),
									'nama'		=>$form_isi->get_nama(),
									'nomor'		=>$form_isi->get_nomor(),
									'keterangan'=>$form_isi->get_keterangan(),
									'grup_id'	=>$form_isi->get_anggota(),
								),
					'grup_array'=>model_grup::read(),
				);

		$this->load->view('theme/theme_kepala');
		$this->load->view('kontak/kontak_formulir', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function ubah_proses()
	{
		$data = $this->input->post();
		$grup_id_array = $data['grup_id'];
		unset($data['grup_id']);

		// update kontak
		model_kontak::update(array('pbk_id'=>$data['pbk_id']),$data);

		// set CRUD
		model_crud::$tabel="pbk_relasi";
		model_crud::$tabel_join=array();

		// menghapus semua relasi
		model_crud::delete(array('pbk_id'=>$data['pbk_id']));

		// mebambahkan relasi
		$relasi = array('pbk_id' => $data['pbk_id']);
		foreach ($grup_id_array as $grup_id) {
			$relasi['grup_id'] = $grup_id;
			model_crud::create($relasi);
		}

		redirect(site_url('/kontak/lihat'), 'refresh');
	}

	# ========== HAPUS ==========

	public function hapus($id="")
	{
		if ($id=="") show_404();

		model_kontak::delete(array('pbk_id'=>$id));

		model_crud::$tabel="pbk_relasi";
		model_crud::$tabel_join=array();
		model_crud::delete(array('pbk_id'=>$id));

		redirect(site_url('/kontak/lihat'), 'refresh');
	}
}

/* End of file utama.php */
/* Location: ./application/controllers/utama.php */